package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.ModuleRecordDao;
import com.sugon.entity.ModuleRecordEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.entity.SysUserEntity;
import com.sugon.service.*;
import com.sugon.utils.*;
import com.sugon.utils.excel.ExcelImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 改良改善台账Service实现类
 *
 * @author sugon
 * @date 2019-08-08 08:37:29
 */
@Service("moduleRecordService")
public class ModuleRecordServiceImpl implements ModuleRecordService {
    @Autowired
    private ModuleRecordDao moduleRecordDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysOssService sysOssService;

    @Override
    public ModuleRecordEntity queryObject(String id) {
        return moduleRecordDao.queryObject(id);
    }

    @Override
    public List<ModuleRecordEntity> queryList(Map<String, Object> map) {
        return moduleRecordDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleRecordDao.queryTotal(map);
    }

    @Override
    public int save(ModuleRecordEntity moduleRecord) {
        moduleRecord.setId(RandomUtil.randomString(32));
        if (StrUtil.isEmpty(moduleRecord.getCreateUser())) {
            moduleRecord.setCreateUser(ShiroUtils.getUserId());
        }
        moduleRecord.setCreateDate(new Date());
        moduleRecord.setDelFlag(1);
        moduleRecord.setDeptCode(ShiroUtils.getDeptId());
        moduleRecord.setApprovalStatus(0);
        sysProcessInfoService.saveProcessInfo(moduleRecord.getId(), ProcessInfoEnum.MDE_GLGS, 0, null);
        int rows = moduleRecordDao.save(moduleRecord);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = moduleRecord.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../module/modulerecord.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.MDE_GLGS, msgEntity, 0);
        }
        return rows;
    }

    @Override
    public int update(ModuleRecordEntity moduleRecord) {
        moduleRecord.setUpdateUser(ShiroUtils.getUserId());
        moduleRecord.setUpdateDate(new Date());

        int rows = moduleRecordDao.update(moduleRecord);
        if (rows > 0) {
            SysMsgEntity msgEntity = new SysMsgEntity();
            String Id = moduleRecord.getId();
            msgEntity.setDataId(Id);
            msgEntity.setDataUrl("../module/modulerecord.html?Id=" + Id);
            sysMsgService.addApproveItem(ProcessInfoEnum.MDE_GLGS, msgEntity, 0);
        }
        return rows;
    }

    @Override
    public int check(ModuleRecordEntity moduleRecord) {
        moduleRecord.setApprovalUser(ShiroUtils.getUserId());
        moduleRecord.setApprovalTime(new Date());

        sysProcessInfoService.saveProcessInfo(moduleRecord.getId(), ProcessInfoEnum.MDE_GLGS, moduleRecord.getApprovalStatus(), null);

        int rows = moduleRecordDao.update(moduleRecord);
        if (rows > 0) {
            SysMsgEntity sysMsgEntity = new SysMsgEntity();
            String Id = moduleRecord.getId();
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataType(ProcessInfoEnum.MDE_GLGS);
            sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
            sysMsgService.allMsgData(sysMsgEntity);
        }
        return rows;
    }


    @Override
    public int delete(String id) {
        return moduleRecordDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return moduleRecordDao.deleteBatch(ids);
    }

    @Override
    public int importE(MultipartFile file, Integer type) {
        //获取所有表格数据
        List<String[]> list = ExcelImport.getExcelData(file);
        int count = 0;
        if (CollectionUtil.isNotEmpty(list)) {
            for (int i = 1; i < list.size(); i++) {
                ModuleRecordEntity mre = new ModuleRecordEntity();
                mre.setId(RandomUtil.randomString(32));
                mre.setCreateUser(ShiroUtils.getUserId());
                mre.setCreateDate(new Date());
                mre.setDelFlag(1);
                mre.setDeptCode(ShiroUtils.getDeptId());
                mre.setType(type);
                String[] array1 = list.get(i);
                int num = 15;
                if (type == 1) {
                    num = 13;
                }
                String[] array = new String[num];
                System.arraycopy(array1, 0, array, 0, array1.length);
                //开始时间
                if (StringUtils.isNotEmpty(array[1])) {
                    String startDateStr = array[1];
                    Date staDate = DateUtils.strToDate(startDateStr);
                    mre.setStartDate(staDate);
                }
                //结束时间
                if (StringUtils.isNotEmpty(array[2])) {
                    String endDateStr = array[2];
                    Date endDate = DateUtils.strToDate(endDateStr);
                    mre.setEndDate(endDate);
                }
                //项目名称
                if (StringUtils.isNotEmpty(array[3])) {
                    mre.setProName(array[3]);
                }
                //项目实施背景
                if (StringUtils.isNotEmpty(array[4])) {
                    mre.setImplBack(array[4]);
                }
                //加工方案
                if (StringUtils.isNotEmpty(array[5])) {
                    mre.setProPlan(array[5]);
                }
                //验证方案
                if (StringUtils.isNotEmpty(array[6])) {
                    mre.setValPlan(array[6]);
                }
                //项目收益
                if (StringUtils.isNotEmpty(array[7])) {
                    mre.setProReve(array[7]);
                }
                //实施方式
                if (StringUtils.isNotEmpty(array[8])) {
                    mre.setImpMode(array[8]);
                }
                //实施单位
                if (StringUtils.isNotEmpty(array[9])) {
                    mre.setExpUnit(array[9]);
                }
                //主要实施人
                if (StringUtils.isNotEmpty(array[10])) {
                    mre.setRepairUserName(array[10]);
                }
                //状态
                if (StringUtils.isNotEmpty(array[11])) {
                    int status = Integer.parseInt(array[11]);
                    mre.setApprovalStatus(status);
                }
                //推广情况
                if (StringUtils.isNotEmpty(array[12])) {
                    mre.setRemark(array[12]);
                }
                if (type == 0) {
                    if (StringUtils.isNotEmpty(array[13])) {
                        mre.setProMethod(array[13]);
                    }
                    if (StringUtils.isNotEmpty(array[14])) {
                        mre.setImpEff(array[14]);
                    }
                }
                count += moduleRecordDao.save(mre);
            }
        }
        return count;
    }

    @Override
    public int importMoldGLGS(MultipartFile file) {
        //获取模具合理化建议表，且长度不得小于11行
        List<String[]> list = ExcelImport.getExcelData(file);
        if (CollectionUtil.isEmpty(list) || list.size() < 11) {
            return 0;
        }
        // 建议名称
        String proName = list.get(1)[1];
        // 员工编号
        String username = list.get(2)[1];
        SysUserEntity sysUserEntity = userService.queryByUserName(username);
        if (sysUserEntity == null) {
            return 0;
        }
        // 节创价值
        String proReve = list.get(4)[1];
        // 建议内容/背景
        String implBack = list.get(5)[1];
        // 实施方案
        String proPlan = list.get(7)[1];
        // 实施效果
        String valPlan = list.get(9)[1];

        ModuleRecordEntity moduleRecord = new ModuleRecordEntity();
        moduleRecord.setProName(proName);
        moduleRecord.setProReve(proReve);
        moduleRecord.setImplBack(implBack);
        moduleRecord.setProPlan(proPlan);
        moduleRecord.setValPlan(valPlan);
        moduleRecord.setCreateUser(sysUserEntity.getUserId());
        moduleRecord.setRepairUserId(sysUserEntity.getUserId());
        moduleRecord.setType(0);
        // 设置路径
        R upload = sysOssService.upload("mold", "file", file);
        moduleRecord.setFileName(upload.get("fileName").toString());
        moduleRecord.setFileUrl(upload.get("url").toString());
        return save(moduleRecord);
    }
}
